package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.home.GoodsDetailModel;
import com.luck.easyfoodbatch.mvp.home.GoodsDetailPresenter;
import com.luck.easyfoodbatch.mvp.home.GoodsDetailView;
import com.luck.easyfoodbatch.mvp.model.GoodDetail;
import com.luck.easyfoodbatch.mvp.model.GoodModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.verticalslide.CustScrollView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.fragment
 * email：893855882@qq.com
 * data：16/9/30
 */
public class ParametersFragment extends BaseFragment implements GoodsDetailView {
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_packing_method)
    TextView tvPackingMethod;
    @BindView(R.id.tv_brand)
    TextView tvBrand;
    @BindView(R.id.tv_series)
    TextView tvSeries;
    @BindView(R.id.tv_flavor)
    TextView tvFlavor;
    @BindView(R.id.tv_commodity_name)
    TextView tv_commodity_name;
    @BindView(R.id.tv_mixed)
    TextView tv_mixed;
    @BindView(R.id.tv_period)
    TextView tv_period;
    @BindView(R.id.tv_manufacture)
    TextView tv_manufacture;
    @BindView(R.id.tv_additive)
    TextView tv_additive;
    @BindView(R.id.tv_factory_name)
    TextView tv_factory_name;
    @BindView(R.id.tv_factory_address)
    TextView tv_factory_address;
    @BindView(R.id.tv_factory_phone)
    TextView tv_factory_phone;
    @BindView(R.id.tv_bar_code)
    TextView tv_bar_code;
    @BindView(R.id.tv_commodity_name_line)
    TextView tvCommodityNameLine;
    @BindView(R.id.tv_mixed_line)
    TextView tvMixedLine;
    @BindView(R.id.tv_manufacture_line)
    TextView tvManufactureLine;
    @BindView(R.id.tv_additive_line)
    TextView tvAdditiveLine;
    @BindView(R.id.tv_num_line)
    TextView tvNumLine;
    @BindView(R.id.tv_packing_method_line)
    TextView tvPackingMethodLine;
    @BindView(R.id.tv_flavor_line)
    TextView tvFlavorLine;
    @BindView(R.id.tv_brand_line)
    TextView tvBrandLine;
    @BindView(R.id.tv_series_line)
    TextView tvSeriesLine;
    @BindView(R.id.tv_factory_name_line)
    TextView tvFactoryNameLine;
    @BindView(R.id.tv_factory_address_line)
    TextView tvFactoryAddressLine;
    @BindView(R.id.tv_factory_phone_line)
    TextView tvFactoryPhoneLine;
    @BindView(R.id.ll_tab1)
    LinearLayout llTab1;
    @BindView(R.id.ll_tab2)
    LinearLayout llTab2;
    @BindView(R.id.ll_tab3)
    LinearLayout llTab3;
    @BindView(R.id.ll_tab5)
    LinearLayout llTab5;
    @BindView(R.id.ll_tab6)
    LinearLayout llTab6;
    @BindView(R.id.ll_tab7)
    LinearLayout llTab7;
    @BindView(R.id.ll_tab8)
    LinearLayout llTab8;
    @BindView(R.id.ll_tab9)
    LinearLayout llTab9;
    @BindView(R.id.ll_tab10)
    LinearLayout llTab10;
    @BindView(R.id.ll_tab11)
    LinearLayout llTab11;
    @BindView(R.id.ll_tab12)
    LinearLayout llTab12;
    @BindView(R.id.ll_tab13)
    LinearLayout llTab13;
    @BindView(R.id.ll_tab14)
    LinearLayout llTab14;
    @BindView(R.id.custom)
    CustScrollView custom;
    @BindView(R.id.iv_nodata)
    ImageView iv_nodata;
    private View rootView;
    private String net_content = "";
    private Handler handler = new Handler();
    private GoodsDetailModel detailModel;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_parameter, container, false);
            ButterKnife.bind(this, rootView);
            detailModel = new GoodsDetailPresenter(this, mContext);
            String gid = getActivity().getIntent().getStringExtra("gid");
            detailModel.goodDetails(1,gid);
        }
        return rootView;
    }

    public void initDataView(GoodDetail.DataBean data) {
        try {
            if (data != null) {
                net_content = data.getNet_content();
                String series = data.getSeries();
                String pack_way = data.getPack_way();
                String brand = data.getBrand();
                String taste = data.getTaste();
                if (!Tools.isNull(net_content)) {
                    tvNum.setText(net_content);
                    llTab6.setVisibility(View.VISIBLE);
                    tvNumLine.setVisibility(View.VISIBLE);
                } else {
                    llTab6.setVisibility(View.GONE);
                    tvNumLine.setVisibility(View.GONE);
                    tvNum.setText("0");
                }
                if (!Tools.isNull(series)) {
                    llTab10.setVisibility(View.VISIBLE);
                    tvSeries.setText(series);
                    tvSeriesLine.setVisibility(View.VISIBLE);
                } else {
                    tvSeries.setText("无");
                    llTab10.setVisibility(View.GONE);
                    tvSeriesLine.setVisibility(View.GONE);
                }
                if (!Tools.isNull(pack_way)) {
                    llTab7.setVisibility(View.VISIBLE);
                    tvPackingMethod.setText(pack_way);
                    tvPackingMethodLine.setVisibility(View.VISIBLE);
                } else {
                    llTab7.setVisibility(View.GONE);
                    tvPackingMethodLine.setVisibility(View.GONE);
                    tvPackingMethod.setText("无");
                }
                if (!Tools.isNull(brand)) {
                    llTab9.setVisibility(View.VISIBLE);
                    tvBrandLine.setVisibility(View.VISIBLE);
                    tvBrand.setText(brand);
                } else {
                    llTab9.setVisibility(View.GONE);
                    tvBrandLine.setVisibility(View.GONE);
                    tvBrand.setText("无");
                }

                if (!Tools.isNull(taste)) {
                    llTab8.setVisibility(View.VISIBLE);
                    tvFlavorLine.setVisibility(View.VISIBLE);
                    tvFlavor.setText(taste);
                } else {
                    llTab8.setVisibility(View.GONE);
                    tvFlavorLine.setVisibility(View.GONE);
                    tvFlavor.setText("无");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置产地
     */
    public void initData(final GoodModel.DataBean data) {
        if (data != null) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    String packageX = data.getPackageX();
                    String country_name = data.getCountry_name();
                    String specifications = data.getSpecifications();
                    if (!Tools.isNull(net_content)) {
                        tvNum.setText(net_content + specifications);
                        tvNumLine.setVisibility(View.VISIBLE);
                        llTab6.setVisibility(View.VISIBLE);
                    } else {
                        tvNum.setText("0" + specifications);
                        tvNumLine.setVisibility(View.GONE);
                        llTab6.setVisibility(View.GONE);
                    }
                }
            }, 100);
        }
    }

    @Override
    public void getGoodsDetailSuccess(GoodDetail model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                GoodDetail.DataBean data = model.getData();
                if (data != null) {
                    custom.setVisibility(View.VISIBLE);
                    iv_nodata.setVisibility(View.INVISIBLE);
                    String bar_code = data.getBar_code();
                    String burden_sheet = data.getBurden_sheet();
                    String product_name = data.getProduct_name();
                    String dataPd = data.getPd();
                    String food_additives = data.getFood_additives();
                    if (!Tools.isNull(bar_code)) {
                        tv_bar_code.setText(bar_code);
                        llTab14.setVisibility(View.VISIBLE);
                    } else {
                        tv_bar_code.setText("无");
                        llTab14.setVisibility(View.GONE);
                    }
                    if (!Tools.isNull(product_name)) {
                        tv_commodity_name.setText(product_name);
                        llTab1.setVisibility(View.VISIBLE);
                        tvCommodityNameLine.setVisibility(View.VISIBLE);
                    } else {
                        tv_commodity_name.setText("无");
                        llTab1.setVisibility(View.GONE);
                        tvCommodityNameLine.setVisibility(View.GONE);
                    }
                    if (!Tools.isNull(burden_sheet)) {
                        tv_mixed.setText(burden_sheet);
                        llTab2.setVisibility(View.VISIBLE);
                    } else {
                        tv_mixed.setText("无");
                        llTab2.setVisibility(View.GONE);
                    }

                    tv_period.setText(data.getExpried_time() + "天");
//                    if (!Tools.isNull(dataPd)) {
//                        tv_manufacture.setText(Tools.SubTime2(dataPd + ""));
//                        llTab4.setVisibility(View.VISIBLE);
//                        tvManufactureLine.setVisibility(View.VISIBLE);
//                    } else {
//                        tv_manufacture.setText("无");
//                        llTab4.setVisibility(View.GONE);
//                        tvManufactureLine.setVisibility(View.GONE);
//                    }

                    if (!Tools.isNull(food_additives)) {
                        tv_additive.setText(food_additives);
                        llTab5.setVisibility(View.VISIBLE);
                        tvAdditiveLine.setVisibility(View.VISIBLE);
                    } else {
                        tv_additive.setText("无");
                        llTab5.setVisibility(View.GONE);
                        tvAdditiveLine.setVisibility(View.GONE);
                    }
                    String factory_name = data.getFactory_name();
                    if (!Tools.isNull(factory_name)) {
                        tv_factory_name.setText(factory_name);
                        llTab11.setVisibility(View.VISIBLE);
                        tvFactoryNameLine.setVisibility(View.VISIBLE);
                    } else {
                        tv_factory_name.setText("无");
                        llTab11.setVisibility(View.GONE);
                        tvFactoryNameLine.setVisibility(View.GONE);
                    }
                    String factory_address = data.getFactory_address();
                    String factory_phone = data.getFactory_phone();
                    if (!Tools.isNull(factory_address)) {
                        tv_factory_address.setText(factory_address);
                        llTab12.setVisibility(View.VISIBLE);
                        tvFactoryAddressLine.setVisibility(View.VISIBLE);
                    } else {
                        tv_factory_address.setText("无");
                        llTab12.setVisibility(View.GONE);
                        tvFactoryAddressLine.setVisibility(View.GONE);
                    }
                    if (!Tools.isNull(factory_phone)) {
                        tv_factory_phone.setText(factory_phone);
                        llTab13.setVisibility(View.VISIBLE);
                        tvFactoryPhoneLine.setVisibility(View.VISIBLE);
                    } else {
                        tv_factory_phone.setText("021-60533381");
                        llTab13.setVisibility(View.VISIBLE);
                        tvFactoryPhoneLine.setVisibility(View.VISIBLE);
                    }
                } else {
                    custom.setVisibility(View.INVISIBLE);
                    iv_nodata.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {

    }
}
